import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-11-10
 * Time: 21:28
 */
public class RedisDemoZSet {

    public static void test1(Jedis jedis) {
        System.out.println("zadd 和 zrange");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        // 多组添加
        jedis.zadd("key", map);

        List<String> members = jedis.zrange("key", 0, -1);
        System.out.println("members:" + members);

        // 获得元素和对应的分数
        // Tuple 类似 Pair，是由 Redis 提供的类
        List<Tuple> membersWithScore = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("membersWithScore:" + membersWithScore);

        // 分别要member和score
        String member = membersWithScore.get(0).getElement();
        double score = membersWithScore.get(0).getScore();
        System.out.println("member:" + member + "\n" + "score:" + score);
    }

    public static void test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();


        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        // 多组添加
        jedis.zadd("key", map);

        long len = jedis.zcard("key");
        System.out.println("len:" + len);
    }

    public static void test3(Jedis jedis) {
        System.out.println("zrem");
        jedis.flushAll();


        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        // 多组添加
        jedis.zadd("key", map);

        long result = jedis.zrem("key", "zhangsan", "lisi");
        System.out.println("result:" + result);

        List<Tuple> tuples = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("tuples:" + tuples);
    }


    public static void test4(Jedis jedis) {
        System.out.println("zscore");
        jedis.flushAll();


        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        // 多组添加
        jedis.zadd("key", map);

        Double score = jedis.zscore("key", "zhangsan");
        System.out.println("score" + score);


    }


    public static void test5(Jedis jedis) {
        System.out.println("zrank");
        jedis.flushAll();


        jedis.zadd("key", 10, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 20.0);
        map.put("wangwu", 30.0);
        // 多组添加
        jedis.zadd("key", map);

        Long rank = jedis.zrank("key", "zhangsan");
        System.out.println("rank:" + rank);

        rank = jedis.zrank("key", "lisi");
        System.out.println("rank:" + rank);
    }


    public static void main(String[] args) {
        try (JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888")) {
            try (Jedis jedis = jedisPool.getResource()) {
                test5(jedis);
            }
        }

    }
}
